<!DOCTYPE html>
<html lang="en">
    <head>
		<meta charset="UTF-8">
		<title>优惠券编辑页面</title>
		<link rel="stylesheet" href="__ADMIN_PATH__/component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form" action="">
			<div class="mainBox">
				<div class="main-container">

					<div class="layui-form-item">
						<label class="layui-form-label"><font color="red">*</font>名称</label>
						<div class="layui-input-block">
							<input type="text" name="name" autocomplete="off" placeholder="请输入优惠券名称" class="layui-input" value="{$info['name'] ?= $info['name']}" />
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label"><font color="red">*</font>优惠券类型</label>
						<div class="layui-input-block">
							{foreach name="coupon_type_array" item="value"}
							    <input type="radio" name="coupon_type" value="{$value['value']}" title="{$value['name']}" {if condition="isset($info['coupon_type']) and $info['coupon_type'] eq $value['value']"}checked=""{/if} />
							{/foreach}
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label"><font color="red">*</font>减免金额</label>
						<div class="layui-input-block">
							<input type="text" name="discount_price" autocomplete="off" placeholder="请输入减免金额" class="layui-input" value="{$info['discount_value'] ?= $info['discount_value']}" />
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label"><font color="red">*</font>到期类型</label>
						<div class="layui-input-block">
							<input lay-filter="expire_type" type="radio" name="expire_type" value="0" title="领取生效" {if condition="isset($info['expire_type']) and $info['expire_type'] eq 0"}checked=""{/if} />
							<input lay-filter="expire_type" type="radio" name="expire_type" value="1" title="固定日期" {if condition="isset($info['expire_type']) and $info['expire_type'] eq 1"}checked=""{/if} />
						</div>
					</div>

					<div class="layui-form-item" {if condition="empty($info) or (!empty($info) and $info['expire_type'] neq 0)"}style="display: none;"{/if} id="expire_type_hour">
						<label class="layui-form-label"><font color="red">*</font>有效小时</label>
						<div class="layui-input-block">
							<input type="text" name="expire_hour" autocomplete="off" placeholder="请输入有效小时(单位 时)" class="layui-input" value="{$info['expire_hour'] ?= $info['expire_hour']}" />
						</div>
					</div>

					<div class="layui-form-item" {if condition="empty($info) or (!empty($info) and $info['expire_type'] neq 1)"}style="display: none;"{/if} id="expire_type_day">
						<label class="layui-form-label"><font color="red">*</font>限时时间</label>
						<div class="layui-input-block">
							<div class="layui-inline">
								<input type="text" id="startTime" name="fixed_time_start" placeholder="请选择限时开始时间" autocomplete="off" class="layui-input" value="{$info['fixed_time_start_time'] ?= $info['fixed_time_start_time']}" />
							</div>
							<div class="layui-inline">
								<input type="text" id="endTime" name="fixed_time_end" placeholder="请选择限时结束时间" autocomplete="off" class="layui-input" value="{$info['fixed_time_end_time'] ?= $info['fixed_time_end_time']}" />
							</div>
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label"><font color="red">*</font>最低金额</label>
						<div class="layui-input-block">
							<input type="text" name="where_order_price" autocomplete="off" placeholder="订单最低金额(只包含服务价格)" class="layui-input" value="{$info['where_order_price'] ?= $info['where_order_price']}" />
						</div>
					</div>
					
					<div class="layui-form-item">
						<label class="layui-form-label">发放总数量</label>
						<div class="layui-input-block">
							<input type="text" name="limit_send_count" autocomplete="off" placeholder="请输入发放总数量(空或0则不限)" class="layui-input" value="{$info['limit_send_count'] ?? ''}" />
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">已发放数量</label>
						<div class="layui-input-block">
							<input type="text" name="already_send_count" autocomplete="off" placeholder="请输入已发放数量(空或0则不限)" class="layui-input" value="{$info['already_send_count'] ?? ''}" />
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label"><font color="red">*</font>发放场景</label>
						<div class="layui-input-block">
							{foreach name="send_scene_array" item="value"}
							    <input type="radio" name="send_scene" value="{$value['value']}" title="{$value['name']}" {if condition="isset($info['send_scene']) and $info['send_scene'] eq $value['value']"}checked=""{/if} />
							{/foreach}
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label"><font color="red">*</font>单次发放数</label>
						<div class="layui-input-block">
							<input type="text" name="one_send_count" autocomplete="off" placeholder="请输入单次发放数量" class="layui-input" value="{$info['one_send_count'] ?= $info['one_send_count']}" />
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">选择城市</label>
						<div class="layui-input-inline" style="width: 80%">
								{foreach name = "proxy" item="value"}

								<input type="checkbox" class="proxy_id" {if in_array($value['proxy_id'],$send)} checked {/if} name="proxy_id" value="{$value['proxy_id']}" title="{$value['company_name']}">

								{/foreach}
								<input type="checkbox"  class="proxy_id"  name="proxy_id" value="999" title="所有城市" {if !empty($info) && $info['send_proxy'] == ''} checked {/if}>


						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">分成承担者</label>
						<div class="layui-input-block">
							<input  type="radio" name="bear_one" value="0" title="同时承担" {if condition="isset($info['bear_one']) and $info['bear_one'] eq 0"}checked=""{/if} />
							<input  type="radio" name="bear_one" value="1" title="平台承担" {if condition="isset($info['bear_one']) and $info['bear_one'] eq 1"}checked=""{/if} />
							<input  type="radio" name="bear_one" value="2" title="技师承担" {if condition="isset($info['bear_one']) and $info['bear_one'] eq 2"}checked=""{/if} />
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">是否启用</label>
						<div class="layui-input-block">
							<input type="checkbox" name="is_enable" lay-skin="switch" lay-text="是|否" {if condition="(isset($info['is_enable']) and $info['is_enable'] eq 1) or (!isset($info['is_enable']))"}checked=""{/if} value="1" />
						</div>
					</div>

				</div>
			</div>

			<div class="bottom">
				<div class="button-container">
					<input type="hidden" name="id" value="{$info['id'] ?= $info['id']}" />
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="coupon">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="pear-btn pear-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>

		</form>
		<script src="__ADMIN_PATH__/component/layui/layui.js"></script>
		<script src="__ADMIN_PATH__/component/pear/pear.js"></script>
		<script>
			layui.use(['form', 'jquery', 'popup', 'loading', 'laydate'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				var popup = layui.popup;
				let loading = layui.loading;
				let laydate = layui.laydate;
				/**
				 * 限制时间
				 */
				var myDate = new Date();  //获取当前时间
				var startTime = laydate.render({
					elem: "#startTime", 
					type: "datetime",
					min:myDate.toLocaleString(),  //转化当前时间格式
					done:function(value,date)
					{
						if(value!=""){
							date.month=date.month-1;
		        			endTime.config.min=date;
				    	}else
				    	{
				    		endTime.config.min=startTime.config.min;
				    	}
					}
				});
				var endTime = laydate.render({
					elem: "#endTime", 
					type: "datetime",
					min:myDate.toLocaleString(),  //转化当前时间格式
					done:function(value,date)
					{
						if(value!=""){
							date.month=date.month-1;
		        			startTime.config.max=date;
			    	    }else
			    	    {
			    	    	startTime.config.max=endTime.config.max;
			    	    }
					}
				});
				/**
				 * 监听到期类型选择
				 */
				form.on('radio(expire_type)', function(data){
					//console.log(data);
					let value = data.value;
					if(value == 0)
					{
						//领取生效
						$('#expire_type_hour').show();
						$('#expire_type_day').hide();
					}else
					{
						//固定日期
						$('#expire_type_hour').hide();
						$('#expire_type_day').show();
					}
				});
				/**
				 * 表单提交
				 */
				form.on('submit(coupon)', function(data) {
					var arr_box = [];

					$('input[class=proxy_id]:checked').each(function() {

						arr_box.push($(this).val());

					});    //数组

					// console.log(arr_box);
					data.field['proxy_id'] = arr_box;
					// return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
					$.ajax({
						url:"{:url('Coupon/save')}",
						type:"POST",
						dataType:"JSON",
						data:data.field,
						beforeSend: function () {
							loading.Load(1, "");
		                },
		                complete: function () {
		                	loading.loadRemove();
		                },
		                success:function(res)
		                {
		                	if(res.code == 0)
		                	{
		                		//正常业务
		                		popup.success(res.msg, function(){
		                			var index = parent.layer.getFrameIndex(window.name);
		                			parent.location.reload();
		                			parent.layer.close(index); //再执行关闭
	                        	});
		                	}else
		                	{
		                		//业务异常
		                		popup.warning(res.msg);
		                	}
		                },
		                error:function()
		                {
		                	loading.loadRemove();
		                	popup.failure('服务器错误');
		                }
		            });
		            return false;
		        });
		    });
		</script>
	</body>
</html>